#!/usr/bin/python3
from py3devil1pld import pyPldHeader
from py3devil1tex import pyTexturePack, pyTextureBatchDescriptor, pyTextureBatch
from py3devil1geo import pyGeoHeader, pyMeshHeader, pyMesh

#print(libc)

#--------------------------------------+
#       Regular Python
#--------------------------------------+

if __name__ == "__main__":
    def pldtest(devil1pld, pldheader):
        with open("pl01.pld", "rb") as f:
            data = f.read()
            devil1pld.getheader(ctypes.byref(pldheader), data)
            devil1pld.printheader(ctypes.byref(pldheader))
           # for offset in pldheader.getoffsets():
           #     print(hex(offset))

    def textest(devil1tex, texheader):
        print("texture test")
        with open("pl01.pld_1.txp", "rb") as f:
            data = f.read()
#            texheader = ctypes.cast(data, ctypes.POINTER(TexturePack))
            th = ctypes.pointer(texheader)
            devil1tex.getheader(ctypes.byref(th), data)
            devil1tex.printheader(th)
            batchdesc = TextureBatchDescriptor()
            bd = ctypes.pointer(batchdesc)
            print("\nbatch descriptor:")
            devil1tex.getbatchdesc(ctypes.byref(bd), 1, data, len(data))
            devil1tex.printbatchdesc(bd)
            print(bd.contents.textureSize)

    def geotest(devil1geo, geoheader):
        print("geo test")
        with open("pl00.pld_0", "rb") as f:
            data = f.read()
#            geoheader = ctypes.cast(data, ctypes.POINTER(Header))
            gh = ctypes.pointer(geoheader)
            devil1geo.getheader(ctypes.byref(gh), data)
            devil1geo.printheader(gh)
            meshheader = MeshHeader()
            mh = ctypes.pointer(meshheader)
            devil1geo.getmeshheader(ctypes.byref(mh), 1, data)
            devil1geo.printmeshheader(mh)

    def main():
        sharedlib='./lib3ddevil1.so'
        libc = ctypes.cdll.LoadLibrary(sharedlib)
        if (not libc):
            print("Couldn't load %s" % sharedlib)
            return 1
        print("OK")

        pldfn = Devil1PLD_FN.in_dll(libc, "DEVIL1PLD")
        pldh = pyPldHeader()
        pldtest(pldfn, pldh)

        texfn = Devil1TEX_FN.in_dll(libc, "DEVIL1TEX")
        texh = TexturePack()
        textest(texfn, texh)

        geofn = Devil1GEO_FN.in_dll(libc, "DEVIL1GEO")
        geoh = Header()
        geotest(geofn, geoh)

    def mainx():
        with open("pl01.pld", "rb") as f:
            data = f.read()
            pld = pyPldHeader(data)
            pld.show()
            pld2 = pyPldHeader()
            pld2.show()

        with open("pl01.pld_1.txp", "rb") as f:
            data = f.read()
            txp = pyTexturePack(data)
            txp.show()
            print(txp.getbatchnumber())
            print(txp.getfirstbatchoffset())
            tbd = pyTextureBatchDescriptor(1, data)
            tbd.show()
            print(tbd.gettexturesize())
            #tx = pyTextures(0, tbd.gettexno(), data)
            tx = pyTextureBatch(0, data)
            ts = tx.gettextures()
            for i in range(0, 10):
                print(ts[0].data[i])

        with open("pl00.pld_0", "rb") as f:
            data = f.read()
            gh = pyGeoHeader(data)
            gh.show()
            print("-------------")
            print(gh.getnummesh())
            print(gh.getunknownb())
            print(gh.getunknownc())
            print(gh.getunknownd())
            print(gh.getpadding())
            print(gh.getunknownoffset())
            mh = pyMeshHeader(3, data)
            print("-------------")
            try:
                mh = pyMeshHeader(87, data)
            except RuntimeError as e:
                print(str(e))
            try:
                mh = pyMesh(87, data)
            except RuntimeError as e:
                print(str(e))
            print("-------------")
            mh.show()
            m = pyMesh(0, data)
            m.show()
            # p = m.getpositions()
            # print("positions:")
            # for point in p:
                # print(point)
            # n = m.getnormals()
            # print("normals:")
            # for point in n:
                # print(point)
            # u = m.getuvs()
            # print("uvs:")
            # for point in u:
                # print(point)
            dbatch = m.getbatchdata()
            print(hex(dbatch.numVertex))
            print(hex(dbatch.padding))
#---------------------------------------+
   # main()
    mainx()
